package com.capgemini.scaffold.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.capgemini.scaffold.common.BusinessException;
import com.capgemini.scaffold.common.ExceptionEnum;
import com.capgemini.scaffold.common.ResponseBean;
import com.capgemini.scaffold.dto.InternDTO;
import com.capgemini.scaffold.entity.Intern;


import com.capgemini.scaffold.enums.GenderEnum;
import com.capgemini.scaffold.enums.JobsEnum;
import com.capgemini.scaffold.enums.StateEnum;
import com.capgemini.scaffold.mapper.InternMapper;
import com.capgemini.scaffold.service.InternService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class InternServiceImpl extends ServiceImpl<InternMapper, Intern> implements InternService {

    @Resource
    private InternMapper internMapper;


    /**
     * 删除学员数据
     * @param id 学员的id
     * @return 返回受影响行数
     */
    @Override
    public int deleteIntren(int id) {
        Intern intern = internMapper.selectById(id);
        //查询删除的数据是否存在
        if (intern == null){
            throw new BusinessException(ExceptionEnum.DELETE_INTERN_INFORMATION);
        }
        int rows = internMapper.deleteById(id);
        //如果受影响行数不为1，返回未知异常
        if (rows != 1){
            throw new BusinessException(ExceptionEnum.DELETE_INTERN_INFORMATIONS);
        }
        return rows;
    }

    /**
     * 实习生数据展示
     * @return 数据集合
     */
    @Override
    public ResponseBean<List<Intern>> queryIntern() {

        List<Intern> internList = internMapper.findIntern();
        //字典查询
        for (Intern record : internList) {
            record.setSexName(GenderEnum.getNameByCode(record.getSex()));
            record.setStateName(StateEnum.getNameByCode(record.getState()));
            record.setJobName(JobsEnum.getNameByCode(record.getJob()));
        }

        return ResponseBean.success(internList);
    }

    //添加实习生信息
    @Override
    public Intern addNewIntern(Intern intern) {


        //通过导师名字更改id字段
        Integer tutorId = internMapper.selectTutorId(intern.getTutorName());
        intern.setTutorId(tutorId);

        //字典替换数字插入
        String Job = JobsEnum.getCodeByName(intern.getJobName());
        System.out.println(Job);
        intern.setJob(Job);

        String state = StateEnum.getCodeByName(intern.getStateName());
        intern.setState(state);

        String sex = GenderEnum.getCodeByName(intern.getSexName());
        intern.setSex(sex);


//        //判断插入数据是否为空
//        if (intern.getName() == null){
//            throw new BusinessException(ExceptionEnum. INSERT_DATA_NULL);
//        }
//        if (intern.getTutorName() == null){
//            throw new BusinessException(ExceptionEnum. INSERT_DATA_NULL);
//        }
//        if (intern.getSex() == null){
//            throw new BusinessException(ExceptionEnum. INSERT_DATA_NULL);
//        }
//        if (intern.getState() == null){
//            throw new BusinessException(ExceptionEnum. INSERT_DATA_NULL);
//        }
//        if (intern.getSchool() == null){
//            throw new BusinessException(ExceptionEnum. INSERT_DATA_NULL);
//        }
//        if (intern.getBatch() == null){
//            throw new BusinessException(ExceptionEnum. INSERT_DATA_NULL);
//        }
//        if (intern.getMajor() == null){
//            throw new BusinessException(ExceptionEnum. INSERT_DATA_NULL);
//        }
//        if (intern.getPhone() == null){
//            throw new BusinessException(ExceptionEnum. INSERT_DATA_NULL);
//        }
//        if (intern.getJob() == null){
//            throw new BusinessException(ExceptionEnum. INSERT_DATA_NULL);
//        }

        // interBatch重复判断
        LambdaQueryWrapper<Intern> qb = Wrappers.lambdaQuery();
        qb.eq(Intern::getBatch, intern.getBatch());
        List<Intern> existInternBatch = internMapper.selectList(qb);
        if (CollectionUtils.isNotEmpty(existInternBatch)) {
            // internPhone重复判断
            LambdaQueryWrapper<Intern> qw = Wrappers.lambdaQuery();
            qw.eq(Intern::getPhone, intern.getPhone());
            List<Intern> existInternPhone = internMapper.selectList(qw);
            // 如果存在则抛出业务异常
            if (CollectionUtils.isNotEmpty(existInternPhone)) {
                throw new BusinessException(ExceptionEnum. INSERT_DATA_DUPLICATION);
            }
        }
        // 插入用户信息
        internMapper.insert(intern);
        return intern;

    }


    /**
     * 修改实习生信息
     * @param intern 修改的数据
     * @return 返回更新操作
     */
    @Override
    public Integer updateIntern(Intern intern) {


        //通过导师名字更改id字段
        Integer integers = internMapper.selectTutorId(intern.getTutorName());
        intern.setTutorId(integers);

        //字典替换数字插入
        String Job = JobsEnum.getCodeByName(intern.getJobName());
        intern.setJob(Job);

        String state = StateEnum.getCodeByName(intern.getStateName());
        intern.setState(state);

        String sex = GenderEnum.getCodeByName(intern.getSexName());
        intern.setSex(sex);

//        //判断修改的数据是否为空
//        if (intern.getName() == null){
//            throw new BusinessException(ExceptionEnum. UPDATE_DATA_NULL);
//        }
//        if (intern.getTutorName() == null){
//            throw new BusinessException(ExceptionEnum. UPDATE_DATA_NULL);
//        }
//        if (intern.getSex() == null){
//            throw new BusinessException(ExceptionEnum. UPDATE_DATA_NULL);
//        }
//        if (intern.getState() == null){
//            throw new BusinessException(ExceptionEnum. UPDATE_DATA_NULL);
//        }
//        if (intern.getSchool() == null){
//            throw new BusinessException(ExceptionEnum. UPDATE_DATA_NULL);
//        }
//        if (intern.getBatch() == null){
//            throw new BusinessException(ExceptionEnum. UPDATE_DATA_NULL);
//        }
//        if (intern.getMajor() == null){
//            throw new BusinessException(ExceptionEnum. UPDATE_DATA_NULL);
//        }
//        if (intern.getPhone() == null){
//            throw new BusinessException(ExceptionEnum. UPDATE_DATA_NULL);
//        }
//        if (intern.getJob() == null){
//            throw new BusinessException(ExceptionEnum. UPDATE_DATA_NULL);
//        }

        //获取state数据
        Intern states = internMapper.selectById(intern.getId());
        //如果学员状态已经优化则无法更改
        if(states.getState().equals(StateEnum.OPTIMIZE.getCode())){
            throw new BusinessException(ExceptionEnum.UPDATE_DATA_ALREADY);
        }
        LambdaQueryWrapper<Intern> qw = Wrappers.lambdaQuery();
        /*
        判断手机号重复之前，忽略和用户信息相同的信息
         */
        qw.ne(Intern::getId,intern.getId())
                //判断电话号码是否重复
            .eq(Intern::getPhone,intern.getPhone());

        List<Intern> existInternAccount = internMapper.selectList(qw);
        //如果电话号码重复则existStudentAccount不为空，抛出业务异常
        if(CollectionUtils.isNotEmpty(existInternAccount)){
            throw new BusinessException(ExceptionEnum.UPDATE_DATA_DUPLICATION);
        }

        //返回更新操作
        return internMapper.updateIntern(intern);
    }

    //根据id获取数据
    @Override
    public Intern selectById(int id) {
        Intern intern = internMapper.selectById(id);
        //字典查询
        intern.setSexName(GenderEnum.getNameByCode(intern.getSex()));
        intern.setJobName(JobsEnum.getNameByCode(intern.getJob()));
        return intern;
    }


    /**
     * 查询用户信息表，入参可以为空
     * name：模糊查询
     * school: 模糊查询
     * major: 模糊查询
     * startTime：CREATE_AT 大于等于 startTime
     * endTime：CREATE_AT 小于 endTime
     * @param req
     * @return
     */
    @Override
    public List<Intern> linkIntern(InternDTO req) {
        System.out.println(req.getName());
        List<Intern> internList = internMapper.linkIntern(req);
        System.out.println(internList);
        //字典查询
        for (Intern record : internList) {
            record.setSexName(GenderEnum.getNameByCode(record.getSex()));
            record.setStateName(StateEnum.getNameByCode(record.getState()));
            record.setJobName(JobsEnum.getNameByCode(record.getJob()));
        }

        return internList;
    }

    /**
     * 分页查询用户信息
     *
     * @param req 查询请求入参
     * @return 查询结果
     */
    @Override
    public IPage<Intern> queryInternPage(InternDTO req) {
        //封装分页信息，第一个参数页码值、第二个参数每页显示数量
        Page<Intern> page = new Page<>(req.getPageNum(), req.getPageSize());
        //进行分页查询
        IPage<Intern> pageList = internMapper.findIntern(page, req);
        //字典循环返回数据
        for (Intern record : pageList.getRecords()) {
            record.setSexName(GenderEnum.getNameByCode(record.getSex()));
            record.setStateName(StateEnum.getNameByCode(record.getState()));
            record.setJobName(JobsEnum.getNameByCode(record.getJob()));
        }
        //获取分req
        return pageList;
    }

    //查询老师姓名
    @Override
    public List<String> selectTutorName() {
        return internMapper.selectTutorName();
    }


}


